package com.sport.setting.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.sport.apis.annotation.DIAccountUserBo;
import com.sport.common.excel.ExcelExport;
import com.sport.common.word.Word;
import com.sport.model.application.pojos.SportMemberEnroll;
import com.sport.model.common.dtos.ResponseResult;
import com.sport.model.common.enums.AppHttpCodeEnum;
import com.sport.model.excel.NullClass;
import com.sport.model.excel.annotation.ExcelField;
import com.sport.model.excel.fieldtype.FieldType;
import com.sport.model.program.dtos.AllocatedGroupDto;
import com.sport.model.program.dtos.AllocatedItemDto;
import com.sport.model.program.pojos.SportProgramMemberItem;
import com.sport.model.program.vos.ItemListVO;
import com.sport.model.program.vos.SheetThreeVO;
import com.sport.model.program.vos.SheetTwoVO;
import com.sport.model.program.vos.WordTwoVo;
import com.sport.model.setting.bos.SportEventsGamesBo;
import com.sport.model.setting.pojos.SportEventsPreEnroll;
import com.sport.model.user.bos.AccountUserBo;
import com.sport.setting.mapper.SportEventsPreEnrollMapper;
import com.sport.setting.mapper.SportMemberEnrollMapper;
import com.sport.setting.mapper.SportProgramMemberItemMapper;
import com.sport.setting.repository.SportEventsGradeRepository;
import com.sport.setting.repository.SportMeetingDefineItemRepository;
import com.sport.setting.repository.SportMemberEnrollRepository;
import com.sport.setting.repository.SportProgramRepository;
import com.sport.setting.service.SportProgramService;
import com.sport.utils.common.BaseUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

@Service
@Slf4j
public class SportProgramServiceImpl implements SportProgramService {

    @Autowired
    SportMemberEnrollMapper sportMemberEnrollMapper;
    @Autowired
    SportMeetingDefineItemRepository sportMeetingDefineItemRepository;
    @Autowired
    SportEventsPreEnrollMapper sportEventsPreEnrollMapper;
    @Autowired
    SportProgramMemberItemMapper sportProgramMemberItemMapper;
    @Autowired
    SportEventsGradeRepository sportEventsGradeRepository;
    @Autowired
    SportMemberEnrollRepository sportMemberEnrollRepository;
    @Autowired
    SportProgramRepository sportProgramRepository;

    @Override
    public List<List<ItemListVO>> unallocatedItem() {
        List<Map<String, Object>> list = sportMemberEnrollMapper.countByGroup(AccountUserBo.getUser().getSchoolTenementId(), AccountUserBo.getUser().getSportId(), 1);
        List<SportEventsGamesBo> eventsGames = sportMeetingDefineItemRepository.getEventsGames(AccountUserBo.getUser().getSchoolTenementId());


        //查询已经分配的项目
        //按成员id分组
        List<Map<String, Object>> memberAllocatedItem = sportProgramMemberItemMapper.getSportMemberAllocatedItem(AccountUserBo.getUser().getSchoolTenementId());
        List<SportProgramMemberItem> sportProgramMemberItems = sportProgramMemberItemMapper.selectList(Wrappers.<SportProgramMemberItem>lambdaQuery().eq(SportProgramMemberItem::getSchoolTenementId,AccountUserBo.getUser().getSchoolTenementId()));
        List<Map<Long, String>> sportMemberAllocatedItem=new ArrayList<>();
        if(memberAllocatedItem!=null&&memberAllocatedItem.size()>0)
           for (Map<String, Object> map : memberAllocatedItem) {
            Map<Long,String> newMap=new HashMap<>();
            Object sportMemberId = map.get("sport_member_id");
            Object sportEventsIds = map.get("sport_events_ids");
            newMap.put(Long.valueOf(sportMemberId.toString()),sportEventsIds.toString());
            sportMemberAllocatedItem.add(newMap);
          }


        //查询比赛项目和对应的id
        Map<Long,String> itemMap=new HashMap<>();
//        for (SportEventsGamesBo eventsGame : sportMeetingDefineItemRepository.getEventsGames()) {
//            itemMap.put(eventsGame.getId(),eventsGame.getSportName());
//        }
        //比赛项目及其对应限制人数
        Map<Long,Integer> limitMap=new HashMap<>();
        List<ItemListVO> unallocatedListVOS=new ArrayList<>();
        List<ItemListVO> allocatedListVOS=new ArrayList<>();

        for (SportEventsGamesBo eventsGame : eventsGames) {
            itemMap.put(eventsGame.getId(),eventsGame.getSportName());
            SportEventsPreEnroll sportEventsPreEnroll = sportEventsPreEnrollMapper.selectById(eventsGame.getId());
            if (sportEventsPreEnroll!=null) limitMap.put(eventsGame.getId(),sportEventsPreEnroll.getMaxPersonLimit());
        }
        for (Map<String, Object> map : list) {
            ItemListVO itemListVO=new ItemListVO();
            String grade = map.get("sport_member_grade").toString();
            itemListVO.setSportEventsGrade(grade);
            String gender =  map.get("sport_member_gender").toString();
            if(gender.equals("1")) {
                itemListVO.setGender("男");
            }else {
                itemListVO.setGender("女");
            }
            long projectId = Long.parseLong(map.get("sport_member_project_id").toString());
            itemListVO.setItem(itemMap.get(projectId));
            String idList =(String) map.get("id_list");
            itemListVO.setIdList(idList);
            String[] split = idList.split(",");
            int count = Integer.parseInt(String.valueOf(split.length));
            itemListVO.setPNum(count);
            //获取该项目的最大限制人数每组
            Integer limit = limitMap.get(projectId);
            int group=0;
            int y=0;
            if (limit !=null) {
                y = count % limit;
                if(y>0){
                    group=count/limit+1;
                }else
                {
                    group=count/limit;
                }
            }

            itemListVO.setGNum(group);
            itemListVO.setSportCompetitionStatus("初赛");
            if(sportMemberAllocatedItem!=null&&sportMemberAllocatedItem.size()>0){
            Q:for (Map<Long, String> longStringMap : sportMemberAllocatedItem) {

                for (Long key : longStringMap.keySet()) {
                    if(itemListVO.getIdList()!=null && itemListVO.getIdList().contains(String.valueOf(key)) && longStringMap.get(key).contains(String.valueOf(projectId))){
                        for (SportProgramMemberItem sportProgramMemberItem : sportProgramMemberItems) {
                            if(sportProgramMemberItem.getSportMemberId()==key&&sportProgramMemberItem.getSportEventsId()==projectId){
                                itemListVO.setSportStartDate(sportProgramMemberItem.getSportStartDate());
                                itemListVO.setSportEventsType(sportProgramMemberItem.getSportEventsType());
                                itemListVO.setSportStartTime(sportProgramMemberItem.getSportStartTime());
                                break;
                            }
                        }
                        allocatedListVOS.add(itemListVO);
                    }else {
                        unallocatedListVOS.add(itemListVO);
                    }
                    break Q;
                }
             }
            }else{
                unallocatedListVOS.add(itemListVO);
            }

        }
        List<List<ItemListVO>> resList=new ArrayList<>();
        resList.add(unallocatedListVOS);
        resList.add(allocatedListVOS);
        return resList;
    }

    @Override
    public List<ItemListVO>  allocatedItem(AllocatedGroupDto allocatedGroupDto) {
        List<ItemListVO> allocatedItemListVOS = allocatedGroupDto.getAllocatedGroupListVOS();
        List<ItemListVO> allocatedListVOS=new ArrayList<>();
        int i=1;
        for (ItemListVO allocatedItemListVO : allocatedItemListVOS) {
            if(allocatedItemListVO.getSportStartDate().equals(allocatedGroupDto.getSportStartDate())
                &&allocatedItemListVO.getSportEventsType().equals(allocatedGroupDto.getSportEventsType())){
                allocatedItemListVO.setId(i++);
                allocatedListVOS.add(allocatedItemListVO);
            }
        }
        return allocatedListVOS;

    }

    @Override
    public ResponseResult updateAllocatedItem(AllocatedItemDto allocatedItemDto) {
        int insert = 0;
        int count = 0;
        List<ItemListVO> allocatedItemListVOS = allocatedItemDto.getAllocatedItemListVOS();
        //查询比赛项目和对应的id
        Map<String, Long> itemMap=new HashMap<>();
        for (SportEventsGamesBo eventsGame : sportMeetingDefineItemRepository.getEventsGames(AccountUserBo.getUser().getSchoolTenementId())) {
            itemMap.put(eventsGame.getSportName(),eventsGame.getId());
        }
        for (ItemListVO allocatedItemListVO : allocatedItemListVOS) {
            if (allocatedItemListVO.getIdList() != null) {
                String[] idList = allocatedItemListVO.getIdList().split(",");
                count+=idList.length;
                for (String s : idList) {
                    SportProgramMemberItem sportProgramMemberItem = BaseUtils.copyProperties(allocatedItemListVO, SportProgramMemberItem.class);
                    sportProgramMemberItem.setSchoolTenementId(AccountUserBo.getUser().getSchoolTenementId());
                    sportProgramMemberItem.setSportEventsId(itemMap.get(allocatedItemListVO.getItem()));
                    sportProgramMemberItem.setSportStartDate(allocatedItemDto.getSportStartDate());
                    sportProgramMemberItem.setSportBindingSchool(Long.valueOf(AccountUserBo.getUser().getSportId()));
                    int flag = sportProgramMemberItemMapper.insert(sportProgramMemberItem);
                    insert+=flag;
                }
            }
        }
        if(insert==count){
            return ResponseResult.okResult("分配成功！");
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    Word wordExport = new Word();
    //创建一个临时文件夹来保存生成的临时文件
    File tempFolder= wordExport.createTempFolder();
    @Override
    public void exportProgram(HttpServletResponse response) {
        String fileName = "秩序册.xlsx";
        String sheetTwoTitle= "参赛队统计";

        try {
            ExcelExport ee = new ExcelExport(sheetTwoTitle, SheetTwoVO.class);
            //创建sheet2内容
            createSheetTwo(ee,tempFolder);
            //创建sheet3内容
            createSheetThree(ee,tempFolder);
            //创建sheet4内容
            createSheetFour(ee,tempFolder);
            //创建sheet5内容
            createSheetFive(ee,tempFolder);
            //写入响应
            ee.write(response, fileName);



        }catch (Exception e){
            e.printStackTrace();
           log.info("秩序册导出失败！");
        }
//        return ResponseResult.okResult("秩序册生成完毕!");
    }

    private void createSheetFive(ExcelExport ee,File tempFolder) throws IOException {
        ee.createSheet(null,"项目分组表", NullClass.class,ExcelField.Type.EXPORT);
        List<List<ItemListVO>> lists = unallocatedItem();
        List<ItemListVO> allocatedListVOS = lists.get(1);
        AllocatedGroupDto allocatedGroupDto = new AllocatedGroupDto();
        allocatedGroupDto.setAllocatedGroupListVOS(allocatedListVOS);
        List<Map<String, String>> sportEventsTimeType = sportProgramMemberItemMapper.getSportEventsTimeType(AccountUserBo.getUser().getSchoolTenementId());
        for (Map<String, String> map : sportEventsTimeType) {
            String sportStartDate = map.get("sport_start_date");
            String sportEventsType = map.get("sport_events_type");
            allocatedGroupDto.setSportStartDate(sportStartDate);
            allocatedGroupDto.setSportEventsType(sportEventsType);
            List<ItemListVO> itemListVOS = allocatedItem(allocatedGroupDto);

            int single=1;
            for (ItemListVO itemListVO : itemListVOS) {
                String[] split = itemListVO.getIdList().split(",");
                List<SportMemberEnroll> memberByIds = sportMemberEnrollRepository.getMemberByIds(split);
                //标题
                Row rowTitle = ee.addRow();
                ee.addCell(rowTitle,2,itemListVO.getSportStartDate());
                ee.addCell(rowTitle,4,itemListVO.getSportEventsType());
                Row rowPreInfo = ee.addRow();
                ee.addCell(rowPreInfo,0,itemListVO.getSportEventsGrade());
                ee.addCell(rowPreInfo,2,itemListVO.getItem());
                ee.addCell(rowPreInfo,5,itemListVO.getSportCompetitionStatus());
                ee.addCell(rowPreInfo,6,itemListVO.getPNum()+"/"+itemListVO.getGNum());
                ee.addCell(rowPreInfo,7,itemListVO.getSportStartTime());
                Row rowTrack = ee.addRow();
                String[] chineseNumbers = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
                for(int i=1;i<9;i++){
                    ee.addCell(rowTrack,i,"第"+chineseNumbers[i]+"道");
                }
                int gNum = itemListVO.getGNum();
                List<Object> showList=new ArrayList<>();
                for(int i=0;i<gNum;i++){
                    Map<String,Object> memberMap=new HashMap<>();
                    Row groupRow1 = ee.addRow();
                    Row groupRow2 = ee.addRow();
                    Row groupRow3 = ee.addRow();
                    ee.addCell(groupRow1,0,"第"+(i+1)+"组");
                    memberMap.put("i",(i+1));
                    int pNum = itemListVO.getPNum();
                    int remainder = pNum % gNum;
                    int columnLast=0;
                    if (remainder>0)
                        columnLast=(pNum-remainder)/gNum+remainder;
                    else{
                        columnLast=pNum/gNum;
                    }
                        for (int j=1;j<=columnLast;j++){
                            if(i *columnLast+ j<=pNum) {
                                ee.addCell(groupRow1, j, memberByIds.get(i * columnLast + j-1).getSportMemberNum());
                                ee.addCell(groupRow2, j, memberByIds.get(i * columnLast + j-1).getSportMemberName());
                                ee.addCell(groupRow3, j, memberByIds.get(i * columnLast + j-1).getSportMemberUnit());
                                memberMap.put("号码" + j, memberByIds.get(i * columnLast + j-1).getSportMemberNum());
                                memberMap.put("姓名" + j, memberByIds.get(i * columnLast + j-1).getSportMemberName());
                                memberMap.put("单位" + j, memberByIds.get(i * columnLast + j-1).getSportMemberUnit());
                            }
                        }
                    showList.add(memberMap);

                }
                //word
                Map<String,Object> finallyMap=new HashMap<>();
                finallyMap.put("日期",itemListVO.getSportStartDate());
                finallyMap.put("类型",itemListVO.getSportEventsType());
                finallyMap.put("组别",itemListVO.getSportEventsGrade());
                finallyMap.put("项目名称",itemListVO.getItem());
                finallyMap.put("赛次",itemListVO.getSportCompetitionStatus());
                finallyMap.put("组数-人数",itemListVO.getPNum()+"/"+itemListVO.getGNum());
                finallyMap.put("时间",itemListVO.getSportStartTime());
                finallyMap.put("列表",showList);
                File wordTemplate ;
                if(single>1){
                    // 下载word模板到指定文件夹 模板1
                     wordTemplate = new File("/docker/java/word/项目分组表模板1.docx");
                }else{
                // 下载word模板到指定文件夹 模板1
                     wordTemplate = new File("/docker/java/word/项目分组表模板.docx");
                }
                // 此处使用了poi-tl的<表格行循环插件>，此处一定要进行参数bind，方便word模板参数替换
                LoopRowTableRenderPolicy policy1 = new LoopRowTableRenderPolicy();
                Configure build = Configure.builder().bind(policy1, "列表")
                        .build();
                generateTemplate(finallyMap,wordTemplate,build,5+itemListVOS.indexOf(itemListVO),tempFolder);
                single++;
            }
        }
    }

    private void createSheetFour(ExcelExport ee, File tempFolder) throws IOException {
        ee.createSheet(null,"竞赛日程",ItemListVO.class,ExcelField.Type.EXPORT);
        List<List<ItemListVO>> lists = unallocatedItem();
        List<ItemListVO> allocatedListVOS = lists.get(1);
        AllocatedGroupDto allocatedGroupDto = new AllocatedGroupDto();
        allocatedGroupDto.setAllocatedGroupListVOS(allocatedListVOS);
        List<Map<String, String>> sportEventsTimeType = sportProgramMemberItemMapper.getSportEventsTimeType(AccountUserBo.getUser().getSchoolTenementId());
        for (Map<String, String> map : sportEventsTimeType) {
            Map<String,Object> finallyMap=new HashMap<>();
            String sportStartDate = map.get("sport_start_date");
            String sportEventsType = map.get("sport_events_type");
            allocatedGroupDto.setSportStartDate(sportStartDate);
            allocatedGroupDto.setSportEventsType(sportEventsType);
            List<ItemListVO> itemListVOS = allocatedItem(allocatedGroupDto);
            Row rowTitle = ee.addRow();
            ee.addCell(rowTitle,2,sportStartDate);
            ee.addCell(rowTitle,4,sportEventsType);
            ee.setDataList(itemListVOS,null);
            //word
            finallyMap.put("日期",sportStartDate);
            finallyMap.put("类型",sportEventsType);
            List<Object> showList=new ArrayList<>();
            setFourList(itemListVOS,showList);
            finallyMap.put("列表",showList);
            // 下载word模板到指定文件夹 模板1
            File wordTemplate = new File("/docker/java/word/竞赛日程模板.docx");
            // 此处使用了poi-tl的<表格行循环插件>，此处一定要进行参数bind，方便word模板参数替换
            LoopRowTableRenderPolicy policy1 = new LoopRowTableRenderPolicy();

            Configure build = Configure.builder().bind(policy1, "列表")
                    .build();
            generateTemplate(finallyMap,wordTemplate,build,4+sportEventsTimeType.indexOf(map),tempFolder);
        }
    }

    private void setFourList(List<ItemListVO> itemListVOS, List<Object> showList) {
        for (ItemListVO itemListVO : itemListVOS) {
            Map<String,Object> memberMap=new HashMap<>();
            memberMap.put("序号",itemListVO.getId());
            memberMap.put("组别",itemListVO.getSportEventsGrade());
            memberMap.put("性别",itemListVO.getGender());
            memberMap.put("比赛项目",itemListVO.getItem());
            memberMap.put("赛次",itemListVO.getSportCompetitionStatus());
            memberMap.put("人数",itemListVO.getPNum());
            memberMap.put("组数",itemListVO.getGNum());
            memberMap.put("时间",itemListVO.getSportStartTime());
            showList.add(memberMap);
        }

    }

    private void createSheetThree(ExcelExport ee,File tempFolder) throws IOException {
        List<SheetThreeVO> sheetThreeVOS = sportProgramRepository.sheetThreeData();
        ee.createSheet(null,"代表队名单", NullClass.class,ExcelField.Type.EXPORT);
        for (SheetThreeVO sheetThreeVO : sheetThreeVOS) {
            Map<String,Object> finallyMap=new HashMap<>();
            List<Object> showMaleList=new ArrayList<>();
            List<Object> showFemaleList=new ArrayList<>();
            //组别和单位
            Row row1 = ee.addRow();
            ee.addCell(row1,1,sheetThreeVO.getGrade());
            ee.addCell(row1,3,sheetThreeVO.getUnit());
            Row row2 = ee.addRow();
            ee.addCell(row2,0,"领队:");
            ee.addCell(row2,1,"");
            Row row3 = ee.addRow();
            ee.addCell(row3,0,"教练:");
            ee.addCell(row3,1,"xxx");
            Row row4 = ee.addRow();
            ee.addCell(row4,0,"男子");
            addCell(ee, sheetThreeVO.getMaleList());
            Row row5 = ee.addRow();
            ee.addCell(row5,0,"女子");
            addCell(ee,sheetThreeVO.getFemaleList());

            //word
            finallyMap.put("比赛组", sheetThreeVO.getGrade());
            finallyMap.put("比赛单位", sheetThreeVO.getUnit());
            setThreeList(sheetThreeVO.getMaleList(),showMaleList);
            setThreeList(sheetThreeVO.getFemaleList(),showFemaleList);
            finallyMap.put("男子列表", showMaleList);
            finallyMap.put("女子列表", showFemaleList);
            // 下载word模板到指定文件夹 模板1
            File wordTemplate = new File("/docker/java/word/代表队名单模板.docx");
            // 此处使用了poi-tl的<表格行循环插件>，此处一定要进行参数bind，方便word模板参数替换
            LoopRowTableRenderPolicy policy1 = new LoopRowTableRenderPolicy();
            LoopRowTableRenderPolicy policy2 = new LoopRowTableRenderPolicy();
            Configure build = Configure.builder().bind(policy1, "男子列表")
                    .bind(policy2,"女子列表").build();
            generateTemplate(finallyMap,wordTemplate,build,3+sheetThreeVOS.indexOf(sheetThreeVO),tempFolder);
        }
    }

    private void generateTemplate(Map<String, Object> finallyMap, File wordTemplate, Configure build,int i,File tempFolder) throws IOException {
        // 进行编译
        //形成临时文件路径
        String tempFilePath=tempFolder.getAbsolutePath()+File.separator+"filled_document_"+i+".docx";
        String absolutePath = wordTemplate.getAbsolutePath();
        XWPFTemplate render = XWPFTemplate.compile(absolutePath, build).render(finallyMap);
        //保存到临时文件
        render.writeToFile(tempFilePath);
        render.close();
    }


    private void setThreeList(List<SportMemberEnroll> list, List<Object> showList) {
        for(int i=0;i<list.size();i+=4){
            Map<String,Object> memberMap=new HashMap<>();
            memberMap.put("号码1",list.get(i).getSportMemberNum());
            memberMap.put("姓名1",list.get(i).getSportMemberName());
            if ((i+1)<list.size()){
            memberMap.put("号码2",list.get(i+1).getSportMemberNum());
            memberMap.put("姓名2",list.get(i+1).getSportMemberName());}
            if((i+2)<list.size()){
            memberMap.put("号码3",list.get(i+2).getSportMemberNum());
            memberMap.put("姓名3",list.get(i+2).getSportMemberName());}
            if((i+3)<list.size()){
            memberMap.put("号码4",list.get(i+3).getSportMemberNum());
            memberMap.put("姓名4",list.get(i+3).getSportMemberName());}
            showList.add(memberMap);
        }

    }

    private static void addCell(ExcelExport ee, List<SportMemberEnroll> list) {
        int column=1;
        Row row = ee.addRow();
        for (SportMemberEnroll sportMemberEnroll : list) {
            if(((column+1)%8)==0){
                row= ee.addRow();
                column=1;
            }
            ee.addCell(row,column,sportMemberEnroll.getSportMemberNum());
            ee.addCell(row,column+1,sportMemberEnroll.getSportMemberName());
            column+=2;
        }
    }

    private void createSheetTwo(ExcelExport ee,File tempFolder) throws IOException {
        List<Map<String, Object>> unitList = sportMemberEnrollMapper.countBySportMemberUnitMaps(AccountUserBo.getUser().getSchoolTenementId());
        List<SheetTwoVO> sheetList = new ArrayList<>();
        //获得每个组的男子和女子总数
        List<Map<String, Object>> gradeList = sportMemberEnrollMapper.countNumByGrade(AccountUserBo.getUser().getSchoolTenementId());
        int id = 1;
        int allMale=0;
        int allFemale=0;
        getSheetOneList(unitList, sheetList, id);
        ee.setDataList(sheetList, null);
        //设置总计单元格
        Row rowT = ee.addRow();
        ee.addCell(rowT,1,"总计", ExcelField.Align.CENTER, FieldType.class,null);
        List<WordTwoVo> wordTwoVos=new ArrayList<>();
        //遍历输出每个组的男子和女子总数
        for (Map<String, Object> map : gradeList) {
            WordTwoVo wordTwoVo = new WordTwoVo();
            Row row = ee.addRow();
            String grade=map.get("sport_member_grade").toString();
            int maleTotalCount = Integer.parseInt(map.get("maleTotalCount").toString());
            int femaleTotalCount = Integer.parseInt(map.get("femaleTotalCount").toString());
            allMale+=maleTotalCount;
            femaleTotalCount+=femaleTotalCount;
            setSportTotal(ee,row,grade,maleTotalCount,femaleTotalCount);
            wordTwoVo.setGrade(grade);
            wordTwoVo.setMaleCount(maleTotalCount);
            wordTwoVo.setFemaleCount(femaleTotalCount);
        }
        //添加运动员总数的单元格
        Row rowAll = ee.addRow();
        setSportTotal(ee, rowAll, "运动会总数", allMale, allFemale);

        //word
        Map<String,Object> finallyMap=new HashMap<>();
        List<Object> showList1=new ArrayList<>();
        List<Object> showList2=new ArrayList<>();
        setTwoShow1List(sheetList,showList1);
        setTwoShow2List(wordTwoVos,showList2);
        finallyMap.put("参赛队列表",showList1);
        finallyMap.put("总计",showList2);
        // 下载word模板到指定文件夹 模板1
        File wordTemplate = new File("/docker/java/word/参赛队统计模板.docx");
        // 此处使用了poi-tl的<表格行循环插件>，此处一定要进行参数bind，方便word模板参数替换
        LoopRowTableRenderPolicy policy1 = new LoopRowTableRenderPolicy();
        LoopRowTableRenderPolicy policy2 = new LoopRowTableRenderPolicy();
        Configure build = Configure.builder().bind(policy1, "参赛队列表")
                .bind(policy2,"总计").build();
        generateTemplate(finallyMap,wordTemplate,build,2,tempFolder);
    }

    private void setTwoShow2List(List<WordTwoVo> wordTwoVos, List<Object> showList2) {
        for (WordTwoVo wordTwoVo : wordTwoVos) {
            Map<String,Object> memberMap=new HashMap<>();
            memberMap.put("组别",wordTwoVo.getGrade());
            memberMap.put("男子","男子:");
            memberMap.put("男子值",wordTwoVo.getMaleCount());
            memberMap.put("女子","女子:");
            memberMap.put("女子值",wordTwoVo.getFemaleCount());
            showList2.add(memberMap);
        }

    }

    private void setTwoShow1List(List<SheetTwoVO> sheetList, List<Object> showList) {
        for (SheetTwoVO sheetTwoVO : sheetList) {
            Map<String,Object> memberMap=new HashMap<>();
            memberMap.put("号码",sheetTwoVO.getId());
            memberMap.put("参赛单位",sheetTwoVO.getSportMemberUnit());
            memberMap.put("组别",sheetTwoVO.getSportMemberGrade());
            memberMap.put("男子号码",sheetTwoVO.getMaleNumRange());
            memberMap.put("女子号码",sheetTwoVO.getFemaleNumRange());
            memberMap.put("男子人数",sheetTwoVO.getMaleCount());
            memberMap.put("女子人数",sheetTwoVO.getFemaleCount());
            showList.add(memberMap);
        }

    }


    private static void setSportTotal(ExcelExport ee, Row rowAll, String title, int male, int female) {
        ee.addCell(rowAll, 2, title, ExcelField.Align.CENTER, FieldType.class, null);
        ee.addCell(rowAll, 4, "男子:", ExcelField.Align.CENTER, FieldType.class, null);
        ee.addCell(rowAll, 5, male, ExcelField.Align.CENTER, FieldType.class, null);
        ee.addCell(rowAll, 7, "女子:", ExcelField.Align.CENTER, FieldType.class, null);
        ee.addCell(rowAll, 8, female, ExcelField.Align.CENTER, FieldType.class, null);
    }

    private static void getSheetOneList(List<Map<String, Object>> list, List<SheetTwoVO> sheetList, int id) {
        for (Map<String, Object> map : list) {
            SheetTwoVO sheetTwoVO = new SheetTwoVO();
            sheetTwoVO.setId(id++);
            sheetTwoVO.setSportMemberUnit(map.get("sport_member_unit").toString());
            sheetTwoVO.setSportMemberGrade(map.get("sport_member_grade").toString());
            sheetTwoVO.setMinSportMemberMaleNum(map.get("min_sport_member_male_num").toString());
            sheetTwoVO.setMaxSportMemberMaleNum(map.get("max_sport_member_male_num").toString());
            sheetTwoVO.setMinSportMemberFemaleNum(map.get("min_sport_member_female_num").toString());
            sheetTwoVO.setMaxSportMemberFemaleNum(map.get("max_sport_member_female_num").toString());
            sheetTwoVO.setMaleCount(Long.parseLong(map.get("maleCount").toString()));
            sheetTwoVO.setFemaleCount(Long.parseLong(map.get("femaleCount").toString()));
            sheetTwoVO.setMaleNumRange(sheetTwoVO.getMinSportMemberMaleNum()+"-"+sheetTwoVO.getMaxSportMemberMaleNum());
            sheetTwoVO.setMaleNumRange(sheetTwoVO.getMinSportMemberFemaleNum()+"-"+sheetTwoVO.getMaxSportMemberFemaleNum());
            sheetList.add(sheetTwoVO);
        }
    }
    @Override
    public void exportWordProgram(HttpServletResponse response , String fileName) {
        //合并临时文件
        //wordExport.mergeTempFiles(tempFolder, response, fileName);
        wordExport.mergeTempFiles(tempFolder, response, fileName);
    }

}